package mango.service.db.task;

import java.util.ArrayList;
import java.util.List;

import mango.orm.Dao;
import mango.orm.DaoFactory;
import mango.orm.entity.Entity;

public abstract class DataStruct {
    
    protected Class<? extends Entity> retType;
    protected List<Object> parameters = new ArrayList<Object>();
    protected Entity parameter;
    protected String connector;
    
    public Dao getDao(int eventId, int subEventId) {
        String sql = null;
        this.parameters.clear();
        switch (eventId) {
        case DBTask.DB_EVENT_EXECUTE:
            sql = this.getExecuteSql(subEventId);
            break;
        case DBTask.DB_EVENT_UPDATE:
            sql = this.getUpdateSql(subEventId);
            break;
        case DBTask.DB_EVENT_SELECT_LIST_ENTITY:
        case DBTask.DB_EVENT_SELECT_LIST_STRING:
        case DBTask.DB_EVENT_SELECT_UNIQUE_ENTITY:
        case DBTask.DB_EVENT_SELECT_UNIQUE_STRING:
            sql = this.getSelectSql(subEventId);
            break;
        default:
            break;
        }
        Dao dao = createDao(sql);
        this.fillParameter(dao);
        return dao;
    }
    
    private Dao createDao(String sql) {
        if(this.retType != null && this.connector != null) {
            return DaoFactory.c(this.connector, sql, this.retType);
        }
        else if(this.retType != null) {
            return DaoFactory.c(sql, this.retType);
        }
        else if(this.connector != null) {
            return DaoFactory.c(this.connector, sql);
        }
        return DaoFactory.c(sql);
    }

    private void fillParameter(Dao dao) {
        if(this.parameter != null) {
            dao.setParameter(this.parameter);
        }
        else if(this.parameters != null && this.parameters.size() > 0) {
            dao.setParameter(this.parameters.toArray());
        }
    }
    
    protected String getSelectSql(int subEventId){
        return "";
    }

    protected String getUpdateSql(int subEventId) {
        return "";
    }

    protected String getExecuteSql(int subEventId) {
        return "";
    }
}
